/**
 * @program: clms-second
 * @description:
 * @author: mitu_echo
 * @create: 2021-07-22 19:21
 **/

package com.second.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.second.common.pojo.R;
import com.second.common.util.JwtUtil;
import com.second.common.util.Payload;
import com.second.system.config.RsaKeyProperties;
import com.second.system.dao.RoleDao;
import com.second.system.pojo.entity.Role;
import com.second.system.pojo.utils.MyAnnotation;
import com.second.system.pojo.vo.ShowAllRoleVO;
import com.second.system.service.RoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RsaKeyProperties rsaKeyProperties;

    @Override
    @MyAnnotation( title = "角色模块",businessType = 0)
    public R showAllRole(ShowAllRoleVO showAllRoleVO) {

        if (showAllRoleVO.getPageno() == null) {
            List<Role> roleList = roleDao.selectList(null);
            return R.success().putListData(roleList);
        } else {
            QueryWrapper queryWrapper = new QueryWrapper();
            if (StringUtils.isNotEmpty(showAllRoleVO.getRolename())) {
                queryWrapper.like("role_name",showAllRoleVO.getRolename());
            }
            PageHelper.startPage(showAllRoleVO.getPageno(),showAllRoleVO.getPagesize());
            List<Role> roleList = roleDao.selectList(queryWrapper);
            PageInfo pageInfo = new PageInfo(roleList);
            return R.success().putListData(roleList).putObject(pageInfo);
        }
    }

    @Override
    @MyAnnotation( title = "角色模块",businessType = 0)
    public R showUserHasRole(Integer userid) {

        List<Integer> roleidList = roleDao.showUserHasRole(userid);
        return R.success().putListData(roleidList);
    }

    @Transactional
    @Override
    @MyAnnotation( title = "角色模块",businessType = 2)
    public R updateUserHasRole(List<Integer> roleidList, Integer userid) {

        // 先删除之前的
        roleDao.deleteUserOldRole(userid);
        // 添加新的
        roleDao.insertUserNewRole(roleidList,userid);
        return R.success();
    }

    @Override
    @MyAnnotation( title = "角色模块",businessType = 1)
    public R addRole(Role role) {

        int i = roleDao.insert(role);
        if (i>0) {
            return R.success();
        }
        return R.error();
    }

    @Override
    @MyAnnotation( title = "角色模块",businessType = 2)
    public R updateRole(Role role) {

        int i = roleDao.updateById(role);
        if (i>0) {
            return R.success();
        }
        return R.error();
    }

    @Override
    @MyAnnotation( title = "角色模块",businessType = 3)
    public R delByRoleid(Integer roleid) {
        int i = roleDao.deleteById(roleid);
        if (i>0) {
            return R.success();
        }
        return R.error();
    }

    @Override
    @MyAnnotation( title = "角色模块",businessType = 2)
    public R updateStatus(Integer roleid, Character status) {
        Role role = new Role();
        role.setRoleId(roleid);
        role.setStatus(status);
        int i = roleDao.updateById(role);
        if (i>0) {
            return R.success();
        }
        return R.error();
    }

    @Override
    @MyAnnotation( title = "角色模块",businessType = 0)
    public R isAdmin(Integer userid) {

        List<Integer> roleidList = roleDao.showUserHasRole(userid);
        for (Integer roleid : roleidList) {
            if (roleid == 1) {
               return R.success().putObject(true);
            }
        }
        return R.success().putObject(false);
    }
}
